package cn.chia.front.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import cn.chia.pojo.Orderitems;
import cn.chia.pojo.Orders;
import cn.chia.pojo.Ordertime;
import cn.chia.service.OrderService;
import cn.chia.service.OrdertimeService;
/*
 * 于辉  前台订单Action
 */
@Controller
public class OrderFrontAction{
	private OrderService orderService;           //订单Service
	private OrdertimeService ordertimeService;   //订单时间Service
	private Orders order;                        //订单集合
	private int id;                              //订单id
	private List<?> list;                        //订单集合
	private Set<?> set;                          //某条订单所含商品明细集合
	private int orderStatus;                     //订单状态
	private String reason;                       //申请退款的原因
	private String itempic;
	
	/*
	 * 于辉 查看所有订单
	 */
	public String list() throws Exception{
		list = orderService.findallfront();
		return "success";
	}
	
	/*
	 * 于辉  根据id查订单
	 */
	public String viewbyid() throws Exception{
		order = orderService.findFrontById(id);
		if(order.getFlag()==2){
			itempic = "http://market.chi-a.cn/commoditymanagement/"+order.getPinId().getTblItems().getItemPic().split(",")[1];
			System.out.println(itempic+"......................");
		}
		if(order.getFlag()==1){
			//订单明细是一个set集合，放到set中
			set = order.getOrderitemses();
			Iterator i = set.iterator(); 
			while(i.hasNext()){
				Orderitems o = (Orderitems) i.next();
				if(o.getTblItems().getItemPic().split(",").length>1){
					String pic = "http://market.chi-a.cn/commoditymanagement/" + o.getTblItems().getItemPic().split(",")[1];
					System.out.println(pic+"<<<<<<<<<<<<<<<<<<<<<<<<<");
					o.getTblItems().setItemPic(pic);
				}
			}
		}
		return "success";
	}
	
	/*
	 * 于辉 根据id进行支付
	 */
	public String payviewbyid() throws Exception{
		order = orderService.findFrontById(id);
		set = order.getOrderitemses();
		return "success";
	}
	
	/*
	 * 于辉 根据订单状态查询订单
	 */
	public String viewbystate() throws Exception{
		if(orderStatus==0){list = orderService.findallfront();}
		if(orderStatus==1){list = orderService.findOrdersByState("未付款");}
		if(orderStatus==2){list = orderService.findOrdersByState("已付款");}
		if(orderStatus==3){list = orderService.findOrdersByState("申请退款");}
		if(orderStatus==4){list = orderService.findOrdersByState("已退款");}
		if(orderStatus==5){list = orderService.findOrdersByState("配送中");}
		if(orderStatus==6){list = orderService.findOrdersByState("交易完成");}
		if(orderStatus==7){list = orderService.findOrdersByState("已取消");}
		return "success";
	}
	
	/*
	 * 于辉 修改订单
	 */
	public String update() throws Exception{
		System.out.println(orderStatus+"????????????????????????");
		//订单付款之后的处理
		if(orderStatus==2){
			order.setState("已付款");
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setPaytime(new Date());
			ordertimeService.update(ot);
		}
		//订单申请退款之后的处理
		if(orderStatus==3){
			order.setState("申请退款");
			order.setRefundreason(reason);
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setRefundtime(new Date());
			ordertimeService.update(ot);
		}
		//订单确认收货之后的处理
		if(orderStatus==6){
			order.setState("交易完成");
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setFinishtime(new Date());
			ordertimeService.update(ot);
		}
		//订单取消之后的处理
		if(orderStatus==7){
			order.setState("已取消");
			Ordertime ot = ordertimeService.findById(order.getOrdertime().getOrdernumber());
			ot.setQuxiaotime(new Date());
			ordertimeService.update(ot);
		}
		orderService.updateFront(order);
		return "success";
	}
	
	/*
	 * 以下为各属性的get、set方法
	 */
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	public Set<?> getSet() {
		return set;
	}
	public void setSet(Set<?> set) {
		this.set = set;
	}
	public int getOrderStatus() {
		return orderStatus;
	}
	public void setOrderStatus(int orderStatus) {
		this.orderStatus = orderStatus;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getItempic() {
		return itempic;
	}

	public void setItempic(String itempic) {
		this.itempic = itempic;
	}

	@Resource(name="orderServiceImpl")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	@Resource(name="ordertimeServiceImpl")
	public void setOrdertimeService(OrdertimeService ordertimeService) {
		this.ordertimeService = ordertimeService;
	}
}
